/**
 * @AStrutsContext.as
 * 
 * @author sodaChen mail:asframe@163.com
 * @version 1.0
 * <br>Copyright (C), 2012 ASFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame AStruts2
 * <br>Date:2012-4-11
 */
package com.asframe.astruts2.context
{
	import com.asframe.astruts2.beans.ViewBean;
	import com.asframe.astruts2.config.ObjectToConfigBean;
	import com.asframe.astruts2.interfaces.INoticeFilter;
	import com.asframe.astruts2.interfaces.IRootContainer;
	import com.asframe.collections.maps.HashMap;
	import com.asframe.collections.maps.IMap;
	import com.asframe.pattern.observer.ISubjects;
	import com.asframe.slf4j.ILogger;
	import com.asframe.slf4j.LoggerFactory;
	import com.asframe.utils.ClassUtils;
	
	/**
	 * AStruts2的上下文，主要在实际应用中使用
	 * @author sodaChen
	 * @Date:2012-4-11w
	 */
	public class AStrutsContext implements IAStrutsContext
	{
		/** 日志输出器 **/
		private var log							:ILogger;
		/** 活动对象的集合，通过get和set获取到制定的Bean对象实例 **/
		private var beanMap						:IMap;
		/** 根容器对象 **/
		private var rootContainer				:IRootContainer;
		/** 拦截器的集合 **/
		private var noticeHoldMap				:IMap;
		/** 主题集合 **/
		private var subjects					:ISubjects;
		/** 通知前面的过滤器 **/
		private var beforeNoticeFitlerList		:Array;
		/** 通知处理结束后的过滤器 **/
		private var afterNoticeFitlerList		:Array;
		/** 是否正在初始化 **/
		private var isIniting					:Boolean;
		/** Class对象转成对应的ViewBean和MediatorBean **/
		private var objToConfig					:ObjectToConfigBean;
		private var astrutsData					:AStrutsData;
		/** 存放View的配置信息 **/
		private var viewBeanMap					:IMap;
		
		/**
		 * 构造器，加载配置文件 
		 * @param xml xml文件或者是xml文件的路径
		 * @param onComplete 完成容器初始化的回调函数
		 */	
		public function AStrutsContext(astrutsData:AStrutsData)
		{
			noticeHoldMap = new HashMap();
			beanMap = new HashMap();
			this.subjects = astrutsData.subjects;
			this.rootContainer = astrutsData.root;
			this.astrutsData = astrutsData;
			log = LoggerFactory.getLogger(AStrutsContext);
			objToConfig = new ObjectToConfigBean();
			viewBeanMap = new HashMap();
			
		}
		public function getRootContainer():IRootContainer
		{
			return rootContainer;
		}
		/**
		 * @inheritDoc
		 */		
		public function registerView(viewName:String,viewClass:Class, ...mediatorClasses):void
		{
			objToConfig.initViewMediorClass(astrutsData,viewName,viewClass,mediatorClasses);
		}
		/**
		 * @inheritDoc
		 */		
		public function unregisterView(viewName:String):void
		{
			var viewBean:ViewBean = viewBeanMap.remove(viewName);
			if(viewBean != null)
			{
				//进行彻底地销毁
			}
		}
		/**
		 * @inheritDoc
		 */	
		public function disposeView(viewName:String):void
		{
			
		}
		/**
		 * @inheritDoc
		 */		
		public function registerBean(bean:*,name:String = null):void
		{
			if(name == null)
			{
				name = ClassUtils.getMinClassName(bean);
			}
			beanMap.put(name,bean);
		}
		/**
		 * @inheritDoc
		 */		
		public function unregisterBean(name:String):void
		{
			beanMap.remove(name);
		}
		/**
		 * @inheritDoc
		 */		
		public function getBean(name:String):*
		{
			var bean:* = beanMap.get(name);
			if(bean == null)
			{
				return null;
			}
			else if(bean is Class)
			{
				return new bean();
			}
			return bean;
		}
		/**
		 * @inheritDoc
		 */		
		public function sendNotice(notice:*,...args):void
		{
			noticeFilterHandler(beforeNoticeFitlerList,notice,args);
			subjects.sendNoticeArg(notice,args);
			noticeFilterHandler(afterNoticeFitlerList,notice,args);
		}
		protected function dispseViewAll(viewBean:ViewBean):void
		{
			if(viewBean.view == null)
			{
				//还没有实例，则销毁注册后的监听事件
			}
			viewBean.view = null;
		}
		/**
		 * 通知过滤器的处理 
		 * @param noticeFilters
		 * @param notice
		 * @param args
		 * 
		 */		
		private function noticeFilterHandler(noticeFilters:Array,notice:*,args:Array):void
		{
			if(noticeFilters != null && noticeFilters.length > 0)
			{
				for each (var noticeFilter:INoticeFilter in args) 
				{
					try
					{
						noticeFilter.doFilter(notice,args,this);
					}
					catch(error:Error)
					{
						var str:String = "前";
						if(noticeFilters == afterNoticeFitlerList)
						{
							str = "后";
						}
						log.error(str + "通知过滤器发生错误：" + error );
					}
				}
			}
		}
		
		private function getNoticeFitlers(isBefore:Boolean):Array
		{
			if(isBefore)
			{
				if(beforeNoticeFitlerList == null)
				{
					beforeNoticeFitlerList = new Array();
				}
				return beforeNoticeFitlerList;
			}
			else
			{
				if(afterNoticeFitlerList == null)
				{
					afterNoticeFitlerList = new Array();
				}
				return afterNoticeFitlerList;
			}
		}
	}
}
